package com.example.script.admin.controller;

import com.example.script.admin.enums.AdminResult;
import com.example.script.admin.enums.ChooseScriptResult;
import com.example.script.admin.enums.ScriptResult;
import com.example.script.admin.model.dto.AdminUserDto;
import com.example.script.admin.model.dto.ChooseScriptDto;
import com.example.script.admin.model.dto.ScriptPublishDto;
import com.example.script.admin.service.AdminUserService;
import com.example.script.admin.service.ScriptService;
import com.example.script.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(value = "AdminController",description = "店家接口")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private ScriptService scriptService;
    @Autowired
    private AdminUserService adminUserService;

    @ApiOperation("注册用户")
    @PostMapping("/result")
    public CommonResult<Integer> result(@RequestBody AdminUserDto adminUserDto){
        AdminResult result=adminUserService.register(adminUserDto);
        if (result==AdminResult.REGISTER_SUCCESS){
            return CommonResult.success(1, result.getMessage());
        }
        return CommonResult.failed(result.getMessage());
    }

    @ApiModelProperty("登录用户")
    @PostMapping("/login")
    public CommonResult<Map<String,String>> login(
            @ApiParam("用户名")@RequestParam("username") String username ,
            @ApiParam("密码")@RequestParam("password") String password){
        Map<String, String> login = adminUserService.login(username, password);
        if (!CollectionUtils.isEmpty(login)){
            return CommonResult.success(login,"登录成功");
        }
        return CommonResult.failed("登录失败,用户名或密码错误");
    }


    @ApiOperation("添加剧本")
    @PostMapping("/addScript")
    public CommonResult<Integer> addScript(@RequestBody ScriptPublishDto param) {
        ScriptResult result = scriptService.addScript(param);
        if (result == ScriptResult.ADD_SUCCESS) {
            return CommonResult.success(1, result.getMessage());
        }
        return CommonResult.failed(result.getMessage());
    }


    @ApiOperation("修改剧本")
    @PostMapping("/updateScript")
    public CommonResult<Integer> updateScript(@RequestBody ScriptPublishDto scriptPublishDto) {
        int result = scriptService.updateScript(scriptPublishDto);
        if (result > 0){
            return CommonResult.success(result, "修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @ApiOperation("删除剧本")
    @PostMapping("/deleteScript")
    public CommonResult<Integer> deleteScript(@RequestBody ScriptPublishDto scriptPublishDto) {
        int result = scriptService.deleteScript(scriptPublishDto);
        if (result > 0) {
            return CommonResult.success(result, "删除成功");
        }
        return CommonResult.failed("删除失败");
    }
    @ApiOperation("修改店家信息")
    @PostMapping("/updateAdminUser")
    public CommonResult<Integer> updateAdminUser(@RequestBody AdminUserDto adminUserDto){
        int result=adminUserService.updateAdminUser(adminUserDto);
        if (result>0){
            return CommonResult.success(result,"修改信息成功");
        }
        return CommonResult.failed("修改失败");
    }
    @ApiOperation("店家确定预约状态")
    @PostMapping("/chooseScript")
    public ChooseScriptResult chooseScript(@RequestBody ChooseScriptDto chooseScriptDto){
        return adminUserService.chooseScript(chooseScriptDto);
    }
}
